package ch.uzh.ifi.sonar.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.ui.PlatformUI;
import org.osgi.framework.Bundle;

public class SonarUtility {

	private final static ResourceBundle resourceBundle = ResourceBundle
			.getBundle("resources/reports"); //$NON-NLS-1$
	
	private final static ResourceBundle settingBundle = ResourceBundle.getBundle("resources/settings");

	/* TODO: first you need to create a dynamic clearcase view and map it into your drive, and manually put into resources/streams.csv
	 * 
	 *  */
	private final static String STREAM_CSV_PATH = "resources/streams.csv";

	/**
	 * Gets a string from the resource bundle. We don't want to crash because of
	 * a missing String. Returns the key if not found.
	 */
	public static String getResourceString(String key) {
		try {
			return resourceBundle.getString(key);
		} catch (MissingResourceException e) {
			return key;
		} catch (NullPointerException e) {
			return "!" + key + "!"; //$NON-NLS-1$ //$NON-NLS-2$
		}
	}
	
	public static String getSettingString(String key) {
		try {
			return settingBundle.getString(key);
		} catch (MissingResourceException e) {
			return key;
		} catch (NullPointerException e) {
			return "!" + key + "!"; //$NON-NLS-1$ //$NON-NLS-2$
		}
	}

	public static void Alert(String title, String content) {
		MessageDialog.open(MessageDialog.INFORMATION, PlatformUI.getWorkbench()
				.getDisplay().getActiveShell(), title, content, SWT.NONE);
	}

	public static List<String> readStreamsHeader() {
		File csvFile = new File(SonarUtility.getAbsolutePath(STREAM_CSV_PATH));
		FileInputStream fis;
		try {
			fis = new FileInputStream(csvFile);
			BufferedReader in = new BufferedReader(new InputStreamReader(fis));
			String firstLine = in.readLine();
			if(firstLine != null)
				return Arrays.asList(firstLine.split(SonarConstant.CSV_SEP));
			else
				return null;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch(IOException e1){
			e1.printStackTrace();
			return null;
		}
	}
	
	public static List<List<String>> readStreamContent(){
		File csvFile = new File(SonarUtility.getAbsolutePath(STREAM_CSV_PATH));
		FileInputStream fis;
		List<List<String>> result = new ArrayList<List<String>>();
		try {
			fis = new FileInputStream(csvFile);
			BufferedReader in = new BufferedReader(new InputStreamReader(fis));
			String line;
			while((line = in.readLine())!= null){
				// if it is the first line
				if(!line.endsWith("Year")){
					String[] thisLine = line.split(SonarConstant.CSV_SEP);
					result.add(Arrays.asList(thisLine));
				}
			}
			return result;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch(IOException e1){
			e1.printStackTrace();
			return null;
		}
	}

	public static String getAbsolutePath(String relativePath) {
		Bundle bundle = Platform.getBundle("ch.uzh.ifi.sonar");
		URL url = FileLocator.find(bundle, new Path(relativePath), null);
		try {
			return FileLocator.toFileURL(url).getPath();
		} catch (IOException e) {
			System.out.println("Exception: " + relativePath
					+ " was not found in Sonar bundle");
			e.printStackTrace();
			return null;
		}
	}

}
